package banwood.com.xiongfasy.activity;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.tencent.bugly.crashreport.CrashReport;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class MyApplication extends Application{
	
	private static MyApplication application;
	private static Context context;
	private static String userName;
	private static String enHao;



	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		application=this;

		ZXingLibrary.initDisplayOpinion(this);

		context=getApplicationContext();

		// 获取当前包名
		String packageName = context.getPackageName();
		// 获取当前进程名
		String processName = getProcessName(android.os.Process.myPid());
		// 设置是否为上报进程
		CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
		strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly

		CrashReport.initCrashReport(getApplicationContext(), "b3f0653c53", false);

	}

	public static String getUserName() {
		return userName;
	}

	public static void setUserName(String userName) {
		MyApplication.userName = userName;
	}

	public static Context getContext(){
		return context;
	}
	
	public static MyApplication getApp(){
		return application;
	}

	public static String getEnHao() {
		return enHao;
	}

	public static void setEnHao(String enHao) {
		MyApplication.enHao = enHao;
	}

	/**
	 * 获取进程号对应的进程名
	 *
	 * @param pid 进程号
	 * @return 进程名
	 */
	private static String getProcessName(int pid) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
			String processName = reader.readLine();
			if (!TextUtils.isEmpty(processName)) {
				processName = processName.trim();
			}
			return processName;
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException exception) {
				exception.printStackTrace();
			}
		}
		return null;
	}

}
